<?php

namespace app\modules\admin\models;

use Yii;
use yii\web\IdentityInterface;
use yii\helpers\Url;
use RedisUtils;
class AdminMenu
{
    public static function Getmenu($role)
    {
        //自动登陆时会调用



        $array2=array(
            "0018"=>["id"=>123,"text"=>"管理员管理","children"=>"1"],
            "0019"=>["id"=>124,"text"=>"基础配置","children"=>"1"]
        );

        if($role==1){
           $array=array(
               "0016"=>["id"=>123,
                   "text"=>"管理员管理",
                   "children"=>array(
                       [
                           "id"=>111,
                           "text"=>"账号管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/backmanage/usermanage/index'])
                       ],
                       [
                           "id"=>333,
                           "text"=>"角色管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/backmanage/rightsmanage/index'])
                       ],
                       [
                           "id"=>333,
                           "text"=>"操作日志",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/backmanage/accesslog/index'])
                       ],
                       [
                           "id"=>333,
                           "text"=>"修改密码",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/backmanage/changepassword/index'])
                       ]

                   )
               ],
               "0017"=>["id"=>123,
                   "text"=>"用户管理",
                   "children"=>array(
                       [
                           "id"=>111,
                           "text"=>"用户信息管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/usermanage/usermaininfo/index'])
                       ],
                       [
                           "id"=>333,
                           "text"=>"操作日志",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/usermanage/accesslog/index'])
                       ],
                       [
                           "id"=>333,
                           "text"=>"修改密码",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/usermanage/changepassword/index'])
                       ]

                   )
               ],

           );
       }elseif($role==2){
           $array=array(
               "0019"=>
                   [
                       "id"=>123,
                       "text"=>"车辆配置",
                       "children"=>array(
                           [
                               "id"=>1,
                               "text"=>"品牌管理",
                               "iconCls"=>"icon-save",
                               "url"=>Url::to(['/carconfig/index/index'])
                           ],
                           [
                               "id"=>2,
                               "text"=>"车系管理",
                               "iconCls"=>"icon-save",
                               "url"=>Url::to(['/carconfig/cars/index'])
                           ],
                           [
                               "id"=>3,
                               "text"=>"车型管理",
                               "iconCls"=>"icon-save",
                               "url"=>Url::to(['/carconfig/carmodels/index'])
                           ],
                           )
                      ],
                [
                   "id"=>123,
                   "text"=>"门店管理",
                   "children"=>array(
                       [
                           "id"=>1,
                           "text"=>"城市管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/storeconfig/city/index'])
                       ],
                       [
                           "id"=>2,
                           "text"=>"机场管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/storeconfig/airport/index'])
                       ],
                       [
                           "id"=>3,
                           "text"=>"门店管理",
                           "iconCls"=>"icon-save",
                           "url"=>Url::to(['/storeconfig/store/index'])
                       ],
                   )
               ]
           );
       }else{

            $array=array(
                "0017"=>["id"=>123,
                    "text"=>"用户列表",
                    "children"=>array(
                        [
                            "id"=>111,
                            "text"=>"信息管理",
                            "iconCls"=>"icon-save",
                            "url"=>Url::to(['/usermanage/usermaininfo/index'])
                        ],
//                        [
//                            "id"=>333,
//                            "text"=>"操作日志",
//                            "iconCls"=>"icon-save",
//                            "url"=>Url::to(['/usermanage/accesslog/index'])
//                        ],
//                        [
//                            "id"=>333,
//                            "text"=>"修改密码",
//                            "iconCls"=>"icon-save",
//                            "url"=>Url::to(['/usermanage/changepassword/index'])
//                        ]

                    )
                ],
            );
        }




        return $array;
    }

    //获取全部的菜单权限项目
    public static function Getmenurole()
    {
        $arraymenu=array(
            "000"=>["category"=>"系统管理","element"=>"menu_sys","menu"=>"0","menus"=>"0"],
            "001"=>["id"=>1,"text"=>"管理员管理","children"=>"0" ,"menu"=>"0"],
            "002"=>["id"=>101,"text"=>"管理员管理","childrenss"=>1,"iconCls"=>"icon-save","url"=>Url::to(['/backmanage/usermanage/index'])],
            "003"=>["id"=>102,"text"=>"角色管理","childrenss"=>1,"iconCls"=>"icon-save","url"=>Url::to(['/backmanage/rightsmanage/index'])],
            "004"=>["id"=>103,"text"=>"访问日志","childrenss"=>1,"iconCls"=>"icon-save","url"=>Url::to(['/backmanage/accesslog/index'])],
            "005"=>["id"=>104,"text"=>"修改密码","childrenss"=>1,"iconCls"=>"icon-save","url"=>Url::to(['/backmanage/changepassword/index'])],
            "006"=>["id"=>2,"text"=>"车辆配置","children"=>"0" ,"menu"=>"0"],
            "007"=>["id"=>201,"text"=>"品牌管理","childrenss"=>2,"iconCls"=>"icon-save","url"=>Url::to(['/carconfig/index/index'])],
            "008"=>["id"=>202,"text"=>"车系管理","childrenss"=>2,"iconCls"=>"icon-save","url"=>Url::to(['/carconfig/cars/index'])],
            "009"=>["id"=>203,"text"=>"车型管理","childrenss"=>2,"iconCls"=>"icon-save","url"=>Url::to(['/carconfig/carmodels/index'])],
            "010"=>["id"=>3,"text"=>"门店配置","children"=>"0" ,"menu"=>"0"],
            "011"=>["id"=>301,"text"=>"城市管理","childrenss"=>3,"iconCls"=>"icon-save","url"=>Url::to(['/storeconfig/city/index'])],
            "012"=>["id"=>302,"text"=>"机场管理","childrenss"=>3,"iconCls"=>"icon-save","url"=>Url::to(['/storeconfig/airport/index'])],
            "013"=>["id"=>303,"text"=>"门店管理","childrenss"=>3,"iconCls"=>"icon-save","url"=>Url::to(['/storeconfig/store/index'])],
            "014"=>["category"=>"会员专区","element"=>"menu_user","menu"=>"1","menus"=>"1"],
            "015"=>["id"=>4,"text"=>"会员管理","children"=>"0" ,"menu"=>"1"],
            "016"=>["id"=>401,"text"=>"会员管理","childrenss"=>4,"iconCls"=>"icon-save","url"=>Url::to(['/usermanage/usermaininfo/index'])],

        );

        return $arraymenu;
    }

    //权限配置
    public static function getPermission()
    {
        $config=array(
            "101"=>["id"=>1100,"text"=>"管理员管理","children"=>"0"],

            "102"=>["id"=>1101,"text"=>"添加管理员","children"=>"1100"],
            "103"=>["id"=>1102,"text"=>'修改管理员',"children"=>"1100"],
            "104"=>["id"=>1103,"text"=>'删除管理员',"children"=>"1100"],
            "105"=>["id"=>1104,"text"=>'用户角色管理',"children"=>"1100"],
            "106"=>["id"=>1105,"text"=>"查看管理员角色","children"=>"1100"],
            "107"=>["id"=>1106,"text"=>"添加管理员角色","children"=>"1100"],


            "108"=>["id"=>1200,"text"=>"角色管理","children"=>"0"],
            "109"=>["id"=>1201,"text"=>"角色列表","children"=>"1200"],
            "110"=>["id"=>1202,"text"=>"添加角色","children"=>"1200"],
            "111"=>["id"=>1203,"text"=>'修改角色',"children"=>"1200"],
            "112"=>["id"=>1204,"text"=>'删除角色',"children"=>"1200"],
            "113"=>["id"=>1205,"text"=>'角色权限列表',"children"=>"1200"],
            "114"=>["id"=>1206,"text"=>'调整角色权限',"children"=>"1200"],
            "115"=>["id"=>1207,"text"=>"操作日志","children"=>"1200"],

            "116"=>["id"=>1300,"text"=>"车辆配置","children"=>"0"],
            "117"=>["id"=>1301,"text"=>"车辆品牌列表","children"=>"1300"],
            "118"=>["id"=>1302,"text"=>"添加品牌","children"=>"1300"],
            "119"=>["id"=>1303,"text"=>"修改品牌","children"=>"1300"],
            "120"=>["id"=>1304,"text"=>"删除品牌","children"=>"1300"],
            "121"=>["id"=>1305,"text"=>"车系列表","children"=>"1300"],
            "122"=>["id"=>1306,"text"=>"车系添加","children"=>"1300"],
            "123"=>["id"=>1307,"text"=>'修改车系',"children"=>"1300"],
            "124"=>["id"=>1308,"text"=>'删除车系',"children"=>"1300"],
            "125"=>["id"=>1309,"text"=>'车型列表',"children"=>"1300"],
            "126"=>["id"=>1310,"text"=>'车型添加',"children"=>"1300"],
            "127"=>["id"=>1311,"text"=>"修改车型","children"=>"1300"],
            "128"=>["id"=>1312,"text"=>"删除车型","children"=>"1300"],
            "129"=>["id"=>1313,"text"=>"车辆配置模板","children"=>"1300"],
            "130"=>["id"=>1314,"text"=>"参数配置","children"=>"1300"],
        );


       return $config;
    }

    /**
     * @//根据管理员的菜单权限设置，从redis中提取对应的菜单
     */
    public static function Getmenubyrole()
    {
        //打开session
        session_start();
        //准备一个数组，接应redis的返回值
        $arraymenureturn=null;
        //预定义管理员编号
        $manageid=0;
        //从session中获取登录者主键编号
        if(isset ($_SESSION["CurrentUserID"]))
            $manageid=$_SESSION["CurrentUserID"];
        else
            return "-1";
        //根据管理员主键编号获取redis中的权限列表
        $josnrole=RedisUtils::get_manager_role($manageid);

        if($josnrole)
        {
            //将json格式数据转换成数组
            $arrayuserinfo=json_decode($josnrole,true);
            //判断redis中获取的数组，是否存在权限数组，因为有可能该管理员没有任何权限设置，就不存在role字段
            if(array_key_exists("role",$arrayuserinfo))
            {
                //从本类中获取所有菜单的配置
                $arraymenuset=AdminMenu::Getmenurole();
                //循环redis中取出的用户权限数组
                foreach($arrayuserinfo["role"] as $key=>$value)
                {
                    //同时循环所有的菜单配置数组
                    foreach($arraymenuset as $key2=>$value2)
                    {
                        //菜单的权限是0~100，一一比对，吧两个数字中同时存在的数据保留出来。
                        if($key<100&&$key==$key2)
                        {
                            $arraymenureturn[]=$value2;
                        }
                    }
                }
            }
            else
                return -1;
        }
        else
            return -1;

        //var_dump($arraymenureturn);exit;
        //准备一个3级菜单的返回数组
        $arraymenus = array();;
        //将保留下来的，属于该用户的权限数组循环，
        foreach($arraymenureturn as $key=>$value) {
            //匹配一级菜单的特征
            if (array_key_exists("menu", $value) && array_key_exists("element", $value)) {
                $arraymenus[] = $value;

            }
        }
        //var_dump($arraymenus);exit;
        //将保留下来的，属于该用户的权限数组循环，
        foreach($arraymenureturn as $key=>$value)
        {
            //匹配二级菜单的特征
                if(array_key_exists("children",$value) && $value["children"]=="0") {
                    //var_dump("ss");
                    foreach ($arraymenus as $key2 => $value2) {

                        if ($value2["menus"] == $value["menu"]) {

                            if (is_array($arraymenus[$key2]["menu"])) {

                                $arraymenus[$key2]["menu"][] = $value;
                            } else {

                                $arraymenus[$key2]["menu"] = array();
                                $arraymenus[$key2]["menu"][] = $value;

                            }
                        }


                    }
                }
        }
        //将保留下来的，属于该用户的权限数组循环，
        foreach($arraymenureturn as $key=>$value)
        {
            //匹配三级菜单的特征
            if(array_key_exists("childrenss",$value) && $value["childrenss"]!="0") {
                foreach($arraymenus as $key3=>$value3)
                {

                    if($value3["menu"]!=0)
                    {
                        foreach($value3["menu"] as $key4=>$value4)
                        {
                            if($value4["id"]===$value["childrenss"])
                            {
                                if(is_array($value4["children"]))
                                {
                                    $arraymenus[$key3]["menu"][$key4]["children"][]=$value;
                                }
                                else
                                {
                                    $arraymenus[$key3]["menu"][$key4]["children"]=array();
                                    $arraymenus[$key3]["menu"][$key4]["children"][]=$value;
                                }
                            }
                        }

                    }
                }
            }

        }

        return $arraymenus;
    }


    /**
     * @//验证管理员操作权限,$arra='101';
     */
    public static function fixactiobyrole($arra)
    {
        //打开session
        session_start();
        //准备一个数组，接应redis的返回值
        $arraymenureturn = null;
        //预定义管理员编号
        $manageid = 0;
        //从session中获取登录者主键编号
        if (isset ($_SESSION["CurrentUserID"]))
            $manageid = $_SESSION["CurrentUserID"];
        else
            return "-1";
        //根据管理员主键编号获取redis中的权限列表
        $josnrole = RedisUtils::get_manager_role($manageid);



        $array=json_decode($josnrole,true);

        if(is_array($array) && $array!=null && array_key_exists("role",$array) && is_array($array["role"]))
        {
            if(array_key_exists($arra,$array["role"]))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

    }/**
 * @//根据管理员的菜单权限设置，从redis中提取对应的菜单
 */

    /**
     * @
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     *@
     */

    public  function  getUser(){
        return $this->user;
    }

    /**
     * @
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * @
     */
    public function validatePassword($password)
    {
        return $this->pwd === $password;
    }

}